package com.technician.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.technician.common.PageRequest;
import com.technician.common.ResponseResult;
import com.technician.exceptionHandler.GlobalSystemException;
import com.technician.mapper.CommentMapper;
import com.technician.model.dto.comment.CommentInsertRequest;
import com.technician.model.dto.comment.CommentQueryRequest;
import com.technician.model.entity.Comment;
import com.technician.model.enums.HttpStatusEnum;
import com.technician.model.vo.CommentVO;
import com.technician.service.CommentService;
import com.technician.service.UserService;
import com.technician.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * (Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-11-16 15:55:37
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(CommentQueryRequest commentQueryRequest) {
        //查询对应商品或预约的根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //判断是那种类型 0 购物  1 预约
        queryWrapper.eq(commentQueryRequest.getType() != null, Comment::getType, commentQueryRequest.getType());
        //根评论 rootid -1
        queryWrapper.eq(Comment::getRootId, -1);
        //判断购物或预约的id
        queryWrapper.eq(commentQueryRequest.getCommentId()!=null, Comment::getCommentId, commentQueryRequest.getCommentId());
        //分页查询
        Page<Comment> commentPage = new Page<>(commentQueryRequest.getCurrent(), commentQueryRequest.getPageSize());
        Page<Comment> page = this.page(commentPage, queryWrapper);

        //分页查询根评论，且类型转化为 CommentVO
        List<CommentVO> commentVoList = BeanCopyUtils.copyBeanList(page.getRecords(),CommentVO.class);

        //获取所有跟评论对应的子评论，并赋值给对应的属性
        for (CommentVO commentVO : commentVoList){
            List<CommentVO> children = getChildren(commentVO.getCommentId());
            commentVO.setChildren(children);
            //为root为-1的CommentVO 添加昵称和头像的数据
            if (commentVO.getRootId()==-1){
            commentVO.setAvatar(userService.getById(commentVO.getCreateBy()).getAvatar());
            commentVO.setNickName(userService.getById(commentVO.getCreateBy()).getNickName());
        }
        }
//

        return ResponseResult.success(HttpStatusEnum.SUCCESS_RESPONSE,commentVoList);
    }

    @Override
    public ResponseResult addComment(CommentInsertRequest commentInsertRequest) {
        //评论内容不能为空
        if (!StringUtils.hasText(commentInsertRequest.getContent())) {
            throw new GlobalSystemException(500, "评论内容不能为空");
        }
        //转换一下类型，然后进行存储
        Comment comment = BeanCopyUtils.copyBean(commentInsertRequest, Comment.class);
//        Comment comment = new Comment();
////        comment.setCommentId(commentInsertRequest.getCommentId());
//        comment.setType(commentInsertRequest.getType());
//        comment.setRootId(commentInsertRequest.getRootId());
//        comment.setGoodsId(commentInsertRequest.getGoodsId());
//        comment.setToCommentId(commentInsertRequest.getToCommentId());
//        comment.setToCommentUserId(commentInsertRequest.getToCommentUserId());
//        comment.setContent(commentInsertRequest.getContent());
//        comment.setCommentId(commentInsertRequest.getCommentId());
//        comment.setCreateTime(commentInsertRequest.getCreateTime());
//        comment.setCreateBy(commentInsertRequest.getCreateBy());

        save(comment);

        return ResponseResult.success(200,"评论成功");
    }


    /**
     * 根据根评论id查询所有对应的子评论的集合
     * @param commentId 根评论id
     * @return
     */
    private List<CommentVO> getChildren(Long commentId) {

        //收集根评论是commentId 的评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,commentId);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        //进行类型转化并赋值
        List<CommentVO> commentVos = toCommentVoList(comments);
        return commentVos;
    }

    private List<CommentVO> toCommentVoList(List<Comment> list) {

        List<CommentVO> commentVoList = BeanCopyUtils.copyBeanList(list,CommentVO.class);


        //通过vo集合
        for (CommentVO commentVo: commentVoList){
            //通过createBy查询用户的昵称并赋值
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setNickName(nickName);
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才进行查询
            if (commentVo.getToCommentUserId()!=-1) {
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVoList;
    }
}
